package com.myapp.ruyixing.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class ShopErrorReportActivity extends BaseActivity implements
		OnClickListener {

	private EditText mInputText;
	private EditText mEmailText;
	private View mLoadingView;

	String mShopid;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mLoadingView.setVisibility(View.GONE);

			Message message = (Message) o;
			if (message != null) {
				Toast.makeText(ShopErrorReportActivity.this, message.msg,
						Toast.LENGTH_SHORT).show();
			}

			if (message.errno == 0) {
				finish();
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			mLoadingView.setVisibility(View.GONE);
			Toast.makeText(ShopErrorReportActivity.this,
					throwable.getMessage(), Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_report_error);

		findViewById(R.id.submit).setOnClickListener(this);

		mInputText = (EditText) findViewById(R.id.feedback);
		mEmailText = (EditText) findViewById(R.id.email);
		mLoadingView = findViewById(R.id.my_loading);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		mShopid = getIntent().getStringExtra(ShopDetailActivity.INTENT_SHOP_ID);
		String shopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);

		if (TextUtils.isEmpty(shopName) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("其他错误\n"
					+ shopName);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			mLoadingView.setVisibility(View.VISIBLE);
			RYHttpClient.sendShopError(this, mHandler, mShopid, mInputText
					.getText().toString(), mEmailText.getText().toString());
			break;

		default:
			break;
		}
	}
}
